package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.app.dto.CouponRecordDto;
import com.yami.shop.bean.model.CouponRecord;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.CouponRecordService;
import jodd.util.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/couponRecord/couponRecord")
public class CouponRecordController {

    @Autowired
    private CouponRecordService couponRecordService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:page')")
    public ResponseEntity<IPage<CouponRecord>> page(CouponRecord couponRecord, PageParam<CouponRecord> page){
        IPage<CouponRecord> couponRecordList = couponRecordService.page(page,new LambdaQueryWrapper<CouponRecord>()
                .eq(CouponRecord::getShopId, SecurityUtils.getSysUser().getShopId())
                .orderByDesc(CouponRecord::getRecordId));
        return ResponseEntity.ok(couponRecordList);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:info')")
    public ResponseEntity<CouponRecord> info(@PathVariable("id") Long id){
        CouponRecord couponRecord = couponRecordService.getById(id);
        return ResponseEntity.ok(couponRecord);
    }

    /**
     * 获取用户可使用的优惠券
     */
    @GetMapping("/couponUse")
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:info')")
    public ResponseEntity<List<CouponRecordDto>> couponUse(CouponRecord couponRecord){
        String userId = SecurityUtils.getUser().getUserId();
        couponRecord.setUserId(userId);
        List<CouponRecordDto> couponRecordList = couponRecordService.selectUseCouponRecord(couponRecord);
        return ResponseEntity.ok(couponRecordList);
    }


    /**
     * 保存
     */
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:save')")
    public ResponseEntity<Map> save(@Valid @RequestBody CouponRecord couponRecord) {
        HashMap map = new HashMap();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String userId = SecurityUtils.getUser().getUserId();
        couponRecord.setUserId(userId);
        couponRecord.setCreateTime(new Date());
        List<CouponRecord> couponRecordList = couponRecordService.getByUserIdAndShopId(couponRecord);
        if(CollectionUtils.isEmpty(couponRecordList) ){

        }else {
            map.put("code",300);
            map.put("message","不可重复领取该券");
            return ResponseEntity.ok(map);
        }

        couponRecordService.save(couponRecord);
        map.put("code",200);
        return ResponseEntity.ok(map);
    }

    /**
     * 修改
     */
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:update')")
    public ResponseEntity<Void> update(@Valid @RequestBody CouponRecord couponRecord){
        couponRecord.setUpdateTime(new Date());
        couponRecordService.updateById(couponRecord);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
//    @PreAuthorize("@pms.hasPermission('coupon:coupon:delete')")
    public ResponseEntity<Void> delete(@RequestBody Long[] ids){
        couponRecordService.removeByIds(Arrays.asList(ids));
        return ResponseEntity.ok().build();
    }
}
